<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总共:{{ total }}条</span>
        </template>
        <template #right>
          <el-button round size="small" type="warning" @click="$router.push('/employees/import')">导入 Excel</el-button>
          <el-button v-allow="'export_employee'" round size="small" type="danger" @click="hExpot">导出 Excel</el-button>
          <el-button round size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 20px">
        <el-table border :data="empolees">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template v-slot="{ row }">
              <!-- <img class="avatar" :src="row.staffPhoto" alt=""> -->
              <image-holder :src="row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{row}">
              {{ formatEmploy(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间">
            <template v-slot="{row}">
              {{ formdata(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button
                type="text"
                @click="$router.push('/employees/detail/' + row.id)"
              >查看</el-button>
              <el-button type="text" @click="hAssignRole(row.id)">分配角色</el-button>
              <el-button type="text" @click="hdel(row.id)">删除</el-button>
            </template>

          </el-table-column></el-table>
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :current-page="q.page"
            :page-sizes="[5, 10, 20, 30]"
            :page-size="q.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="hSizeChange"
            @current-change="hCurrentChange"
          />
        </div></el-card></div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="新增"
      width="50%"
      :visible.sync="showDialog"
    >
      <emp-dialog v-if="showDialog" @susscss="hsucss" @close="showDialog=false" />
    </el-dialog>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="分配角色"
      width="50%"
      :visible.sync="showDialogRole"
    >
      <assign-role v-if="showDialogRole" :id="turId" @cloed="showDialogRole=false" />
    </el-dialog>
  </div>
</template>
<script>
import { delEmployee, getEmployeeList } from '@/api/employ'
import employeesConst from '@/constant/employees'
import empDialog from '@/views/employees/empDialog.vue'
import assignRole from '@/views/employees/assignRole.vue'
import dayjs from 'dayjs'
const employmap = {}
employeesConst.hireType.forEach(item => {
  employmap[item.id] = item.value
})
export default {
  components: {
    empDialog,
    assignRole
  },
  data() {
    return {
      empolees: [],
      total: 0,
      q: {
        page: 1,
        size: 10
      },
      showDialog: false,
      showDialogRole: false,
      turId: ''

    }
  },
  created() {
    this.loadempoless()
  },
  methods: {
    async  loadempoless() {
      const res = await getEmployeeList(this.q)
      this.total = res.data.total
      this.empolees = res.data.rows
    },
    formatEmploy(code) {
      return employmap[code]
    },
    hSizeChange(size) {
      this.q.size = size
      this.loadempoless()
    },
    hCurrentChange(page) {
      this.q.page = page
      this.loadempoless()
    },
    async hdel(id) {
      try {
        // 提醒用户
        await this.$confirm('真的要删除吗?嘤嘤嘤!', '提示', { type: 'warning' })
        // const result = await this.$confirm('真的要删除吗?嘤嘤嘤!', '提示', { type: 'warning' })
        // .catch(e => e)
        // if (result !== 'confirm') return
        // 发请求
        const res = await delEmployee(id)
        // 根据结果提醒用户
        this.$message.success(res.message)
        // 判断: 当前是否为最后一条数据, 并且当前页 > 1, 需要 -1
        if (this.empolees.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        // 更新列表
        this.loadempoless()
      } catch (e) {
        // console.log(e)
        // 精细化处理异常
        if (e === 'cancel') return
        this.$message.error(e.message)
      }
    },
    hsucss() {
      this.showDialog = false
      this.total++
      this.q.page = Math.ceil(this.total / this.size)
      this.loadempoless()
    },
    formdata(data) {
      return dayjs(data).format('YYYY-MM-DD')
    },
    async hExpot() {
      const mapInfo = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const res = await getEmployeeList({ page: 1, size: this.total })
      // console.log(res)
      const list = res.data.rows

      const zhList = list.map(enobj => {
        const zhobj = {}
        const enkeys = Object.keys(enobj)
        enkeys.forEach(enkey => {
          const zhkey = mapInfo[enkey]
          zhobj[zhkey] = enobj[enkey]
        })
        return zhobj
      })

      const first = zhList[0]
      if (!first) return
      const header = Object.keys(first)
      const data = zhList.map(item => {
        const code = item['聘用形式']
        item['聘用形式'] = employmap[code]
        return Object.values(item)
      })
      const excel = await import('@/vendor/Export2Excel')
      excel.export_json_to_excel({
        header,
        data,
        filename: '芜湖陈氏大公司',
        autoWidth: true,
        bookType: 'xlsx'
      })
    },
    hAssignRole(id) {
      this.turId = id
      this.showDialogRole = true
    }
  }
}
</script>
<style lang="scss" scoped>
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.el-pagination {
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
